package me.lorenzop.arduinolink;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;

public class ArduinoNetworking {

@SuppressWarnings("unused")
	private final ArduinoLink plugin;

	private String host = "";
	private int    port = 23;

	private HashMap<Integer, String> devices = new HashMap<Integer, String>();

	public ArduinoNetworking(ArduinoLink plugin, String host, int port) {
		this.plugin = plugin;
		this.host = host;
		this.port = port;
	}


	public void sendMessage(String data){
		if(host.isEmpty() || port<1) return;
		try{
			if(data.length() < 9){data+="        ";}
			int port = 23;
			// connect to host
			Socket client = new Socket(host, port);
			OutputStream out = client.getOutputStream();
//			InputStream in = client.getInputStream();
			// send message
			out.write(data.getBytes());
			out.write(10);
			out.flush();
			// close
//			in.close();
			out.close();
			client.close();
		}catch(UnknownHostException unknownHost){
			ArduinoLink.log.info(ArduinoLink.logPrefix + "Unknown Host: " + unknownHost.getMessage() );
			return;
		}catch(IOException e){
			ArduinoLink.log.info(ArduinoLink.logPrefix + e.getMessage() );
			return;
		}catch(Exception e){
			ArduinoLink.log.info("Problem initializing client socket!");
			e.printStackTrace();
			return;
		}
	}


	public int countDevices() {
		return devices.size();
	}


}
